﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Evolution.Structure.Globe.Fields;
using EvolutionBaseType;
using System.ComponentModel;

namespace Evolution.Structure.Globe
{
    public class World
    {
        public Field[,] worldFields;
        public int Size {get;set;}

        
        public World(int size)
        {
             worldFields = new Field[size+1,size+1];
             Size = size;
        }
        public Field GetFieldByPosition(Point p)
        {
            try
            {
                return worldFields[p.X, p.Y];
            }
            catch (Exception) { throw new InvalidOperationException(); }
        }
        public void setField(Point p, Field field)
        {
                worldFields[p.X, p.Y] = field;

        }

        /// <summary>
        /// Returns a part of the world. using the position as the center and the mapsize as the length of the sides of the square
        /// </summary>
        /// <param name="position">Point of origin (mostlikely a city)</param>
        /// <param name="mapsize">the length of the sides of the square around the Position. Needs to be uneven.</param>
        /// <param name="influenceArea">Determines whether this is for the city influenced area or not</param>
        /// <returns></returns>
        public BindingList<Field> getWorldFieldList(Point position, int mapsize, bool influenceArea)
        {

            ///HIER MUSS NOCH DIE MAPSIZE NACH GERADE UND UNGERADE ABGEFRAGT WERDEN

            int startIndexX = position.X - ((mapsize - 1) / 2);
            int startIndexY = position.Y - ((mapsize - 1) / 2);


            int endIndexX = startIndexX + mapsize;
            int endIndexY = startIndexY + mapsize;


            //MAYBE startt diesem hier leere Felder einbauen!

            //if (startIndexX < 0)
            //    startIndexX = 0;
            //if (startIndexY < 0)
            //    startIndexY = 0;

            BindingList<Field> result = new BindingList<Field>();
            for (int i = startIndexX; i < endIndexX; i++)
            {
                for (int j = startIndexY; j < endIndexY; j++)
                {
                    if (i < 0 || j < 0)
                    {
                        result.Add(new Field());
                    }
                    else
                    {
                        //set to influenced by city
                        if (influenceArea)
                            worldFields[i, j].InfluencedByCity = true;

                        result.Add(worldFields[i, j]);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// Returns the whole world as a BindingList
        /// </summary>
        /// <returns></returns>
        public BindingList<Field> getWorldFieldList()
        {
            BindingList<Field> result = new BindingList<Field>();
            for (int i = 0; i < Size; i++)
            {
                for (int j = 0; j < Size; j++)
                {
                    result.Add(worldFields[i,j]);
                }
            }

            return result;
        }
    }
}
